package framework.impl;

import framework.api.KnowledgeBuilderResult;
import framework.api.Message;
import framework.api.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MessageImpl implements Message {

    protected static final transient Logger logger = LoggerFactory.getLogger(MessageImpl.class);

    private final long id;
    private final Level level;
    private final String path;
    private final int line;
    private final int column;
    private final String text;

    private String kieBaseName;

    public MessageImpl(long id,
                       Level level,
                       String path,
                       String text) {
        this.id = id;
        this.level = level;
        this.path = path;
        this.text = text;
        this.line = 0;
        this.column = 0;
    }

    public MessageImpl(long id,
                       KnowledgeBuilderResult result) {
        this.id = id;
        switch ( result.getSeverity() ) {
            case ERROR:
                level = Level.ERROR;
                break;
            case WARNING:
                level = Level.WARNING;
                break;
            default:
                level = Level.INFO;
        }
        //See JIRA DROOLS-193 (KnowledgeBuilderResult does not always contain a Resource)
        Resource resource = result.getResource();
        if ( resource == null ) {
            logger.debug( "resource is null: " + result.toString() );
            path = null;
        } else {
            path = resource.getSourcePath();
        }

        if ( result.getLines().length > 0 ) {
            line = result.getLines()[ 0 ];
        } else {
            line = -1;
        }
        column = 0;
        text = result.getMessage();
    }

    public void setKieBaseName( String kieBaseName ) {
        this.kieBaseName = kieBaseName;
    }
}
